﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IISDeploy.Controller;
using IISDeploy.Configuration;
using IISDeploy.Domain.Core;
using IISDeploy.Properties;
using IISDeploy.Views;

namespace IISDeploy
{
    public partial class MainForm : Form
    {
        private ObjectExplorerViewControl objectViewControl;
        private MainHandler mainHandler;

        public MainForm()
        {
            InitializeComponent();
            InitializeController();           
        }

        private void UpdateUI()
        {
            this.Update();
            UpdateStatusBar();
            UpdateTreeList();
            CreateObjectViewComponent();
        }

        private void CreateObjectViewComponent()
        {
            objectViewControl = new ObjectExplorerViewControl();
            this.pagObjectExplorer.Controls.Add(objectViewControl);

        }

        private void UpdateStatusBar()
        {
            lblVersion.Text = Application.ProductVersion;
            lblOsVersion.Text = Environment.OSVersion.VersionString;
            stbMain.Update();
        }

        private void UpdateTreeList()
        {
            trwServers.BeginUpdate();
            try
            {
                trwServers.Nodes.Clear();
                TreeNode topNode = CreateTopNode();
                CreateServersNode(topNode);
                topNode.ExpandAll();
            }
            finally
            {
                trwServers.EndUpdate();
            }
        }

        private void CreateServersNode(TreeNode topNode)
        {
            foreach (Server server in mainHandler.GetServerList())
            {
                TreeNode serverNode = CreateServerNode(server);
                topNode.Nodes.Add(serverNode);
            }
        }

        private TreeNode CreateServerNode(Server server)
        {
            TreeNode serverNode = new TreeNode(server.Name, 1, 1);
            serverNode.Tag = server.Id;
            return serverNode;
        }

        private TreeNode CreateTopNode()
        {
            return trwServers.Nodes.Add(Resources.ResourceManager.GetString("strNetwork"),
                                        Resources.ResourceManager.GetString("strNetwork"), 0);
        }

        private void InitializeController()
        {
            mainHandler = DependencyContainer.Instance.Resolve<MainHandler>();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            UpdateWaitText();
            UpdateUI();
        }

        private void UpdateWaitText()
        {
            trwServers.Nodes.Add(Resources.ResourceManager.GetString("strWaitLoadData"));
            trwServers.Update();
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }        
    }
}
